﻿#region License
//  Distrubted Under the GNU Public License version 2 (GPLv2)
// ========================================
// 
//  QuizzMasterStudio Version 3 
//  Copyright © 2012 , The Sparks (Autism App Jam 2014)
//  All rights reserved.  
// 
// Robert Long, Zephyr Yao, Hillary Chansavang, Li Xiao, Arial Xiao
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//  
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//  The full license is also included in the root folder.
// 
// Robert Long
//  Email: rbtLong@live.com
#endregion

using System;
using System.ComponentModel;
using System.Windows.Input;
using Core.Annotations;
using Core.Features.FeatureBase;
using Core.User;
using Core.Util;

namespace Core.Features.Journal.ViewModel
{
    public class UserJournalVM : INotifyPropertyChanged, IFeature
    {
        private const string _askSentence = "How is your day so far?";
        private readonly JournalEntry _journal = new JournalEntry();
        private readonly JournalScorer _scorer;

        public string Entry
        {
            get { return _journal.Entry; }
            set { _journal.Entry = value; }
        }
        public string AskSentence { get { return _askSentence; } }
        public JournalEntry Journal { get { return _journal; } }
        public string JournalCount
        {
            get
            {
                return string.Format("Archieved: {0}", 
                    UserProfileSession.GetSession().User.Journal.Count);
            }
        }

        public event EventHandler MoodChanged = delegate { }; 
        public event PropertyChangedEventHandler PropertyChanged = delegate { };
        public event EventHandler Finished = delegate { }; 

        public UserJournalVM()
        {
            _scorer = new JournalScorer(_journal, 
                UserProfileSession.GetSession().User);
        }


        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, 
                new PropertyChangedEventArgs(propertyName));
        }

        public ICommand CmdSad
        {
            get
            {
                return new CommandHandler(o =>
                {
                    _journal.Mood = Moods.Sad;
                    MoodChanged.Invoke(this, new EventArgs());
                });
            }
        }

        public ICommand CmdSoso
        {
            get
            {
                return new CommandHandler(o =>
                {
                    _journal.Mood = Moods.Soso;
                    MoodChanged.Invoke(this, new EventArgs());
                });
            }
        }

        public ICommand CmdHappy
        {
            get
            {
                return new CommandHandler(o =>
                {
                    _journal.Mood = Moods.Happy;
                    MoodChanged.Invoke(this, new EventArgs());
                });
            }
        }

        public ICommand CmdSubmit
        {
            get
            {
                return new CommandHandler(o =>
                {
                    Finished(this, new EventArgs());
                    persistToSession();
                });
            }
        }

        public ICommand CmdBackToMenu
        {
            get
            {
                return new CommandHandler(o => Finished(this, new EventArgs()));
            }
        }

            private void persistToSession()
            {
                var prof = UserProfileSession.GetSession().User;
                prof.Journal.Add(Journal);
                prof.AppendScore(_scorer.CalculateScore());
                UserProfileSession.SaveSession();
            }
    }
}
